﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Asistente.Managers
{
    public class Numerator
    {
        //Clase encargada de generar nombres secuenciales a partir de un texto o el tipo de un objeto

        private static Numerator _instance;
        private List<string> Names = new List<string>();
        private List<int> LastNumbers = new List<int>();

        private Numerator()
        {
            // Intentionally blank
        }

        public static Numerator Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Numerator();
                }
                return _instance;
            }
        }

        public String GetName(string name)
        {
            int index = Names.IndexOf(name);
            if (index < 0)
            {
                index = Names.Count;
                Names.Add(name);
                LastNumbers.Add(0);
            }

            LastNumbers[index] += 1;
            return name + LastNumbers[index].ToString();
        }

        public string GetName(MemberInfo type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            return GetName(type.Name);
        }

        public void Reset()
        {
            Names = new List<string>();
            LastNumbers = new List<int>();
        }
    }
}